<template>
    <div class="app-container">
        <div class="search-container">
            <el-form ref="queryParamsRef" :inline="true" :model="queryParams" label-width="100px">
                <el-form-item label="患者姓名" prop="name">
                    <el-input
                        v-model="queryParams.name"
                        clearable
                        placeholder="请输入患者姓名"
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item label="住院号" prop="hospitalNo">
                    <el-input
                        v-model="queryParams.hospitalNo"
                        clearable
                        placeholder="请输入住院号"
                        @keyup.enter="handleQuery"
                    />
                </el-form-item>
                <el-form-item>
                    <query-form-button @handleQuery="handleQuery" @resetQuery="resetQuery"/>
                    <el-button type="success" @click="bedCardPrint">
                        <template #icon>
                            <i-ep-printer/>
                        </template>
                        打印
                    </el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table v-adaptive="390" :border="true" :data="patientInfoList"
                  :header-cell-style="$headerCellStyle"
                  :stripe="true" highlight-current-row
                  @selection-change="handleSelectionChange">
            <el-table-column :align="'center'" type="selection" width="40"/>
            <el-table-column :align="'center'" label="房号" prop="roomNo" width="80"/>
            <el-table-column :align="'center'" label="床号" prop="bedNo" width="80">
                <template #default="scope">
                    <span class="ys-css">{{ scope.row.bedNo }}</span>
                </template>
            </el-table-column>
            <el-table-column :align="'left'"
                             :header-align="'center'" label="患者信息" prop="name" width="145">
                <template #default="scope">
                    {{ scope.row.name }}
                    <span style="margin-left: -10px">
                            <svg-icon :icon-class="dictStore.getMateDictInfo(
                                dictDataMap['sys_user_sex'],
                                scope.row.sex
                            ).dictValue === '1'
                                ? 'man'
                                : 'woman'
                                " style="margin-left: 10px"/>
                        </span>
                    {{ scope.row.age }} 岁{{ scope.row.ageMonth }} 月
                    <!-- {{ scope.row.ageDay }}天 -->
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="住院号" prop="hospitalNo" width="160">
                <template #default="scope">
                    <span class="ys-css">{{ scope.row.hospitalNo }}</span>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="入院时间" prop="admissionTime" width="150">
                <template #default="scope">
                    <span>{{ $parseTime(scope.row.admissionTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
                </template>
            </el-table-column>
            <el-table-column :align="'center'" label="诊断" prop="diagnosis" width="260"/>
            <el-table-column :align="'center'" label="管床医生" prop="doctorInChargeName" width="120"/>
            <el-table-column :align="'center'" label="责任护士" prop="dutyNurseName" width="120"/>
            <el-table-column :align="'center'" label="患者所在科室" prop="deptStayedName" width="100"/>
            <el-table-column :align="'center'" label="患者所在病区" prop="wardCodeName" width="100"/>
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import {bedCardList} from "@/api/hospitalization/patVisit.ts"
import {useUserStoreHook} from "@/store/modules/user.ts";
import {useDictStoreHook} from "@/store/modules/dict.ts";

const {proxy} = getCurrentInstance() as any;
const currentSelectDept = proxy.$currentSelectDept
const useUserStore = useUserStoreHook();
// 登录用户信息
const userInfo = <UserInfoBusi>useUserStore.user.userInfo;
const dictStore = useDictStoreHook();
let dictDataMap = ref(dictStore.isTypeGetData('sys_user_sex'))
const objectUrl = ref("")
const patientInfoList = ref<any>([])
const ids = ref<any>([])
const queryParams = ref<any>({
    wardCode: currentSelectDept,
    visitIdArray: ids.value,
    orgId: userInfo.orgId,
    visitId: ''
})

function handleQuery() {
    bedCardListFun()
}

function resetQuery() {

}

function bedCardListFun() {
    bedCardList(queryParams.value).then(res => {
        patientInfoList.value = res.data
    })
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.visitId);
}

function propsBedCardListFun(visitId: string) {
    queryParams.value.visitId = visitId;
    bedCardListFun()
}


/**
 * 医嘱瓶签打印
 */
function bedCardPrint() {

    if (ids.value.length <= 0) {
        return ElMessage.error("请勾选需要打印的患者信息")
    }
    let url = "/business/patVisit/bedCardPrint";
    const formData = new FormData()
    formData.append("visitIdArrayStr", JSON.stringify(ids.value))
    formData.append("templateCode", "bedsideCard")
    formData.append("orgId", queryParams.value.orgId)
    // pdf打印 、打印预览
    proxy.$printPdfPublic(url, formData).then(res => {
        if (res) {
            objectUrl.value = URL.createObjectURL(res)
        } else {
            console.error('打印失败')
        }
    })
}

defineExpose({
    propsBedCardListFun,
    bedCardListFun
})
</script>


<style scoped>

</style>
